#include "cadtvremote.h"

static QStringList LIST_NAME = (QStringList() << "电视" << "频道" << "音量");
static QMultiMap<QString,QString> MAP_BUTTON = QMap<QString,QString>();

CADTvRemote::CADTvRemote(QObject *parent):CAudioControlDevice(parent)
{
    if(MAP_BUTTON.size() ==0)
    {
        MAP_BUTTON.insertMulti("11","开");
        MAP_BUTTON.insertMulti("12", "关");
        MAP_BUTTON.insertMulti("13", "直播");
        MAP_BUTTON.insertMulti("21", "信号源");
        MAP_BUTTON.insertMulti("22", "小聚键");
        MAP_BUTTON.insertMulti("23", "设置");

        MAP_BUTTON.insertMulti("52", "上一个");
        MAP_BUTTON.insertMulti("52", "上个");
        MAP_BUTTON.insertMulti("52", "上一频道");

        MAP_BUTTON.insertMulti("32", "下个");
        MAP_BUTTON.insertMulti("32", "下一个");
        MAP_BUTTON.insertMulti("32", "下一频道");


        MAP_BUTTON.insertMulti("41", "左");
        MAP_BUTTON.insertMulti("41", "往左");

        MAP_BUTTON.insertMulti("43", "右");
        MAP_BUTTON.insertMulti("43", "往右");

        MAP_BUTTON.insertMulti("42", "OK");
        MAP_BUTTON.insertMulti("42", "确认");
        MAP_BUTTON.insertMulti("42", "确定");

        MAP_BUTTON.insertMulti("61", "返回");
        MAP_BUTTON.insertMulti("62", "主页");
        MAP_BUTTON.insertMulti("62", "菜单");

        MAP_BUTTON.insertMulti("72", "音量加");
        MAP_BUTTON.insertMulti("82", "音量减");

    }
}

void CADTvRemote::addDevice(DEVICE_OPERATION *d)
{
    _device_list.append(d);
}

void CADTvRemote::clearDevice()
{
    foreach (DEVICE_OPERATION *d, _device_list) {
        d->deleteLater();
    }

    _device_list.clear();
}

DEVICE_OPERATION * CADTvRemote:: parseWord(QString word)
{
    QString op,name,value;
    qDebug() << "CADTvRemote: parseWord " << word ;

    foreach (QString s, LIST_NAME) {
        if(word.contains(s))
        {
            name = LIST_NAME.at(0);
            op = DEVICE_ACTION_CLICK;
            break;
        }
    }

    QStringList keys =  MAP_BUTTON.keys();
    for(int i=0;i<keys.size();i++){
        QStringList wl = MAP_BUTTON.values(keys.at(i));
        foreach (QString w, wl) {
            if(word.contains(w,Qt::CaseInsensitive))
            {
                value = keys.at(i);
                break;
            }
        }
    }

    if(name.isEmpty() || value.isEmpty())
    {
        return nullptr;
    }

    qDebug()<< "Audio: found device " << name;
    DEVICE_OPERATION *device_op = new DEVICE_OPERATION();
    device_op->_audio_name = name;
    device_op->_audio_operation = op;
    device_op->_audio_value = value;
    device_op->_audio_name_en = "tv";

    return device_op;
}

bool CADTvRemote::actionAudionControl(DEVICE_OPERATION *devOp)
{
    devOp->_match = false;

    foreach (DEVICE_OPERATION *d, _device_list) {
        if(d->_name.contains(devOp->_audio_name) || d->_name.contains(devOp->_audio_name_en) )
        {
            devOp->_id = d->_id;
            devOp->_name = d->_name;
            devOp->_match = true;

            return true;
        }
    }

    return false;
}
